/* eslint-disable */
import axios from 'axios'
import config from '../config/index'
import { Toast } from "mand-mobile"
var service = axios.create({
    baseURL: config.heartBeatURL,
    timeout: 5000
})
//添加请求拦截器
service.interceptors.request.use(function (config) {
    console.log(config)
    // if (store.getters.token) {
    //     config.headers['TOKEN'] = localStorage.getItem("token")
    // }
    return config
}, function (error) {
    return Promise.reject(error)
})
//添加响应拦截器
service.interceptors.response.use(function (response) {
    /**
    * 下面的注释为通过在response里，自定义code来标示请求状态
    * 当code返回如下情况则说明权限有问题，登出并返回到登录页
    * 如想通过xmlhttprequest来状态码标识 逻辑可写在下面error中
    */
    return response
}, function (error) {
    console.log(error.response)
    if (!error.response) {
        Toast.failed("服务器连接异常");
    }
    if (error.response.status == 401) {
        Toast.failed("微信鉴权失败")
    } else if (error.response.status == 400) {
        Toast.failed(error.response.data)
    }
    else {
        Toast.failed(error.response.data.msg)
    }
    return Promise.reject(error)
}
)
export default {
    //get请求
    get(url, param, headers) {
        return new Promise((resolve, reject) => {
            service({
                method: 'get',
                url,
                params: param,
                headers: headers
            }).then(res => {  //axios返回的是一个promise对象
                resolve(res)  //resolve在promise执行器内部 
            }).catch(err => {
                console.log(err, '异常')
            })
        })
    },
    //post请求
    post(url, param, headers) {
        return new Promise((resolve, reject) => {
            service({
                method: 'post',
                url,
                data: param,
                headers: headers
            }).then(res => {
                resolve(res)
            }).catch(err => {
                console.log(err, '异常')
            })
        })
    },
    //post请求
    put(url, param, headers) {
        return new Promise((resolve, reject) => {
            service({
                method: 'PUT',
                url,
                data: param,
                headers: headers
            }).then(res => {
                resolve(res)
            }).catch(err => {
                console.log(err, '异常')
            })
        })
    },
    //post请求
    delete(url, param, headers) {
        return new Promise((resolve, reject) => {
            service({
                method: 'delete',
                url,
                params: param,
                headers: headers
            }).then(res => {
                resolve(res)
            }).catch(err => {
                console.log(err, '异常')
            })
        })
    }
}